package com.example.mingyue.utils;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * 用于token生成与验证
 */
public class JwtUtils {

    private static String signKey = "mingyue";
    private static Long expire = 43200000L;
    //生成jwt
    public static String generateJwt(Map<String, Object> claims){
        System.out.println("开始");
        String jwt = Jwts.builder()
                .addClaims(claims)//添加自定义内容
                .signWith(SignatureAlgorithm.HS256, signKey)
                .setExpiration(new Date(System.currentTimeMillis() + expire))//令牌的有效期
                .compact();

        System.out.println("结束");
        return jwt;
    }
    //解析jwt
    public static Claims parseJWT(String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;


    }
}
